package cn.com.xxlibrary.action;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import cn.com.xxlibrary.orm.ManagerInfoBean;
import cn.com.xxlibrary.service.IBookService;
import cn.com.xxlibrary.service.ISearchService;
import cn.com.xxlibrary.util.UtilTools;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@ParentPackage("myStruts")
@Namespace(value = "/")
public class SearchAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource
	private ISearchService searchService;
	@Resource
	private IBookService bookService;

	private String op = "enter";
	private String wd;
	private Integer st;
	private int pageNo = 1, pageSize = 10;

	private int bt, bd;

	@Action(value = "search", results = {
			@Result(name = SUCCESS, location = "/books/bookSearch.jsp"),
			@Result(name = "Guest", location = "/Guest/search.jsp") })
	public String search() {
		if ("search".equals(op)) {
			int start = (pageNo - 1) * pageSize;
			Map map = searchService.search(bt, bd, st, wd, start, pageSize);
			ActionContext.getContext().getSession()
					.put("SESSION_SEARCH_RESULTS", map.get("queryResult"));
			ActionContext.getContext().getSession()
					.put("SESSION_SEARCH_PAGESIZE", pageSize);
			ActionContext.getContext().getSession()
					.put("SESSION_SEARCH_PAGENO", pageNo);
			ActionContext.getContext().getSession()
					.put("SESSION_SEARCH_COUNT", map.get("count"));
		} else {
			ActionContext.getContext().getSession()
					.put("SESSION_SEARCH_RESULTS", null);
		}
		// 获取当前用户类型，如果是游客，则返回Guest，跳转到游客查询页面
		ManagerInfoBean manager = (ManagerInfoBean) ActionContext.getContext()
				.getSession().get("SESSION_MANAGER_LOGIN");
		if (UtilTools.isNotNullorEmpty(manager)&&!"游客".equals(manager.getManType().getManTypeName())){
			return SUCCESS;
		}else{
			return "Guest";
		}
	}

	// ////////////////////////////////////////////////////

	public ISearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(ISearchService searchService) {
		this.searchService = searchService;
	}

	public String getOp() {
		return op;
	}

	public void setOp(String op) {
		this.op = op;
	}

	public String getWd() {
		return wd;
	}

	public void setWd(String wd) {
		this.wd = wd;
	}

	public Integer getSt() {
		return st;
	}

	public void setSt(Integer st) {
		this.st = st;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public IBookService getBookService() {
		return bookService;
	}

	public void setBookService(IBookService bookService) {
		this.bookService = bookService;
	}

	public int getBt() {
		return bt;
	}

	public void setBt(int bt) {
		this.bt = bt;
	}

	public int getBd() {
		return bd;
	}

	public void setBd(int bd) {
		this.bd = bd;
	}

}
